package com.bf.xpdd.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.bf.xpdd.domain.ActivityBean;
import com.bf.xpdd.domain.ActivityGlobal;
import com.bf.xpdd.domain.ActivityItemBean;
import com.bf.xpdd.domain.SiteSeller;

/**
 * @author linmu
 *
 */
public class ActivityUtility
{
	/**
	 * 从session中获取User
	 */
	public static SiteSeller getSellerFromSession(HttpSession session)
	{
		Object obj = session.getAttribute("seller");
		SiteSeller user = null;
		if (null != obj)
		{
			user = (SiteSeller) obj;
		}
		return user;
	}

	/**
	 * 从session中获取全局属性
	 */
	public static ActivityGlobal getGlobalFromSession(HttpSession session)
	{
		Object obj = session.getAttribute("global");
		ActivityGlobal global = null;
		if (null != obj)
		{
			global = (ActivityGlobal)obj;
		}
		return global;
	}
	
	/**
	 * 获取活动状态
	 */
	public static String getActDesc(ActivityBean bean)
	{
		int actStatus = bean.getStatus();
		// 活动开始时间
		Date beginTime = bean.getBeginTime();
		Date endTime = bean.getEndTime();
		Date currentTime = new Date();
		String activityDesc = "活动未知状态";
		
		//判断当前时间\开始时间\结束时间前后顺序
		if(currentTime.before(beginTime))
		{
			int d = DateUtility.getIntervalDays(currentTime, beginTime);
			activityDesc = "距离活动开始还有" + d + "天";
		}
		else if (currentTime.before(endTime))
		{
			int d = DateUtility.getIntervalDays(beginTime, currentTime);
			int d1 = DateUtility.getIntervalDays(currentTime, endTime);
			activityDesc = "活动已经开始" + d + "天,距离活动结束还有" + d1 + "天";
		}
		else
		{
			int d = DateUtility.getIntervalDays(endTime, currentTime);
			activityDesc = "活动已经结束" + d + "天";
		}

		return activityDesc;
	}
	
	/**
	 * 记录Debug日志
	 */
	public static void debugLogger(Logger logger, Map<String, String> paramters)
	{
		if (logger.isDebugEnabled())
		{
			for (Entry<String, String> entry : paramters.entrySet())
			{
				logger.debug(entry.getKey() + "=" + entry.getValue());
			}
		}
	}

	/**
	 * 从客户端传过来的参数中获取活动的实例
	 * @param paramters
	 * @return
	 */
	public static ActivityBean getActBeanFromParamters(Map<String, String> paramters,
			int xpddItemSize, SiteSeller seller)
	{
		ActivityBean bean = new ActivityBean();
		//活动的范围
		//String actScope = paramters.get("actScope");
		//bean.setActivityScope(Integer.parseInt(actScope));
		bean.setActivityScope(0);
		//活动名称
		bean.setAname(paramters.get("activityName"));
		//活动开始\结束时间
		try
		{
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date beginDate = df.parse(paramters.get("beginTime"));
			bean.setBeginTime(beginDate);
			Date endDate = df.parse(paramters.get("endTime"));
			Calendar cal = Calendar.getInstance();
			cal.setTime(endDate);
			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
			bean.setEndTime(cal.getTime());
		}
		catch (ParseException e)
		{
			//活动的参数错误,返回null
			return null;
		}
		//活动参与的最大人数
		String maxNum = paramters.get("activityNum");
		bean.setMaxNum(Integer.parseInt(maxNum));
		//活动是否可偷听
		bean.setEave("true".equals(paramters.get("isEave")));
		//活动是否可收藏
		bean.setFavorsite("true".equals(paramters.get("isFavorites")));
		//活动是否开启帮助
		bean.setHelp("true".equals(paramters.get("isHelp")));
		//活动使用的模板信息(对应模板表t_sitn_setting.id)
		String modelId = paramters.get("modelId");
		bean.setModleId(Long.parseLong(modelId));
		//活动的自定义信息
		bean.setNotice(paramters.get("notice"));
		//活动是否分享
		bean.setShare("true".equals(paramters.get("isShare")));
		//活动的创建人
		bean.setNick(seller.getNick());
		//活动中商品的数量
		bean.setXpddItemSize(xpddItemSize);
		// 需要记录卖家信息,对应外健//id_seller=t_sitn_seller.id
		// 这里暂时没有实现
		//bean.setSellerId(1);
		//活动状态,默认状态为未开始
		bean.setStatus(Constant.ACTIVITY_STATUS_BEFORE);
		//活动的成交人数,默认值
		bean.setDealNum(0);
		//活动参数的人数
		bean.setUserSize(0);
		//活动创建时间
		bean.setCreateTime(new Date());
		return bean;
	}
	

	/**
	 * 从参数中获取变量修改活动实例
	 */
	public static void modifyActFromParamters(Map<String, String> paramters, ActivityBean bean)
	{
		//活动名称
		String actName = paramters.get("activityName");
		bean.setAname(actName);
		try
		{
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date beginDate = df.parse(paramters.get("beginTime"));
			bean.setBeginTime(beginDate);
			Date endDate = df.parse(paramters.get("endTime"));
			Calendar cal = Calendar.getInstance();
			cal.setTime(endDate);
			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
			bean.setEndTime(cal.getTime());
		}
		catch (ParseException e)
		{
			//活动的参数错误,返回null
			bean.setBeginTime(new Date());
			bean.setEndTime(new Date());
		}
		//参数模板
		String modelId = paramters.get("modelId");
		bean.setModleId(Long.parseLong(modelId));
		//最大活动人数
		String maxNum = paramters.get("activityNum");
		bean.setMaxNum(Long.parseLong(maxNum));
	}

	/**
	 * 获取前台页面新增的活动商品记录
	 * @param itemIdList 数据库中原有的商品id
	 * @param idList	前台页面传入的商品id
	 * @param aid	活动编号
	 */
	public static List<ActivityItemBean> getNewActItemList(List<Long> itemIdList, ArrayList<Long> idList, long aid)
	{
		//复制页面商品id集合
		ArrayList<Long> tmpIdList = new ArrayList<Long>();
		tmpIdList.addAll(idList);
		//新增的商品集合
		tmpIdList.removeAll(itemIdList);
		
		List<ActivityItemBean> itemList = new ArrayList<ActivityItemBean>();
		for (Long itemId : tmpIdList)
		{
			ActivityItemBean item = new ActivityItemBean();
			item.setItemId(itemId.longValue());
			// 此时的bean中的id为数据库中自增的置
			item.setActivityId(aid);
			itemList.add(item);
		}
		return itemList;
	}

	/**
	 * 获取修改时删除的商品id集合
	 */
	public static List<Long> getDelActItemIdList(List<Long> itemIdList, ArrayList<Long> idList)
	{
		//复制页面商品id集合
		ArrayList<Long> tmpIdList = new ArrayList<Long>();
		tmpIdList.addAll(itemIdList);
		//新增的商品集合
		tmpIdList.removeAll(idList);
		return tmpIdList;
	}

	/**
	 * 从参数中获取页面定义的全局属性
	 */
	public static ActivityGlobal getGlobalFromParamters(Map<String, String> paramters, int status)
	{
		ActivityGlobal global = new ActivityGlobal();
		try
		{
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date beginDate = df.parse(paramters.get("beginTime"));
			global.setBeginTime(beginDate);
			Date endDate = df.parse(paramters.get("endTime"));
			Calendar cal = Calendar.getInstance();
			cal.setTime(endDate);
			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
			global.setEndTime(cal.getTime());
			//System.out.println(scope);
			//global.setRange(scope);
			global.setNotice(paramters.get("notice"));
			
			global.setEditTime(new Date());
			int scope = Integer.parseInt(paramters.get("status"));
			global.setStatus(scope);

			if (status == 102)
			{
				global.setId(Long.parseLong(paramters.get("globalId")));
			}
		}
		catch (ParseException e)
		{
			//活动的参数错误,返回null
			return null;
		}
		return global;
	}

	/**
	 * 获取察看信息页面的rightCode,对应modifyGlobalStatus
	 */
	public static int getRightCode(Date beginTime, Date endTime)
	{
		Date nowTime = new Date();
		if(nowTime.before(beginTime) || endTime == null || nowTime.after(endTime))
		{
			return 1;
		}
		else
		{
			return 3;
		}
	}
}
